package com.bidevalution.controller.api;

import com.bidevalution.business.service.BeReplyEnrollService;
import com.bidevalution.business.service.FaceCheckService;
import com.bidevalution.dao.entity.BeReplyEnroll;
import com.qtp.core.model.Response;
import com.qtp.core.util.GsonUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;


@RestController
@RequestMapping("/facecheck")
public class FaceCheckController {
	private Logger logger = Logger.getLogger(getClass());
    @Autowired
    private FaceCheckService faceCheckService;
    @Autowired
    private BeReplyEnrollService beReplyEnrollService;

    private final static String downloadUrl = "http://218.244.141.107:2084/qtp-file-server/UploadedFile";
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    @ResponseBody 
    public Response<String> upload(@RequestParam(value = "file", required = true) MultipartFile file,String card,String name,String replyEnrollId) {
    	return faceCheckService.check(file,card,name,replyEnrollId);
    }
    @RequestMapping(value = "/download", method = RequestMethod.POST)
	public String redirectDownload(HttpSession session,@RequestParam("logId") String logId) {
		return "redirect:" + downloadUrl + "/" ;
	}
    @RequestMapping(value = "/callback", method = RequestMethod.POST)
	public String callback(HttpServletRequest request) {
    	InputStream is = null;
        try
        {
            is = request.getInputStream();
            StringBuilder sb = new StringBuilder();
            byte[] b = new byte[4096];
            for (int n; (n = is.read(b)) != -1;)
            {
                sb.append(new String(b, 0, n));
            }
            logger.info("callback#########"+sb);
            Map<String, Object> infoMap = (Map<String, Object>)GsonUtil.jsonToMapWithGson(sb.toString());
            Map<String, Object> dataMap = (Map<String, Object>)infoMap.get("data");
    		String streamId = String.valueOf(dataMap.get("streamId"));
    		String fileUrl = String.valueOf(dataMap.get("fileUrl"));
    		logger.info(dataMap);
    		logger.info("streamId:"+streamId);
    		BeReplyEnroll beReplyEnroll = beReplyEnrollService.selectByStreamId(streamId);
    		if(beReplyEnroll!=null) {
    			beReplyEnroll.setDownloadUrl(fileUrl);
    			beReplyEnroll.setFileUrl(fileUrl);
    			beReplyEnrollService.updateIdentity(beReplyEnroll);
    		}else {
    			logger.error("beReplyEnroll is null");
    		}
    	 }
        catch (Exception e)
        {
            logger.error(e.getMessage(),e);
        }
        finally
        {
            if (null != is)
            {
                try
                {
                    is.close();
                }
                catch (IOException e)
                {
                    logger.error(e.getMessage(), e);
                }
            }
        }
		return "ok" ;
	}
}
